package 每日一题;

import org.junit.Test;

import java.util.HashMap;

public class 分数到小数 {
    public String fractionToDecimal(int numerator, int denominator) {
       long a = numerator;
       long b = denominator;
        StringBuilder builder = new StringBuilder();
        if ((numerator>0 && denominator<0) || (numerator<0 && denominator>0))
            builder.append("-");
        a=Math.abs(a);
        b=Math.abs(b);
        builder.append(a/b);
        //a比b大
        if (a%b==0)
            return builder.toString();
        builder.append(".");
        HashMap<Long, Integer> map = new HashMap<>();
        while ((a=(a%b)*10)>0 && !map.containsKey(a)){
             map.put(a,builder.length());
             builder.append(a/b);
        }
        if (a==0)
            return builder.toString();
        return builder.insert(map.get(a).intValue(),"(").append(")").toString();
    }
    @Test
    public void test(){
        long a=1;
        long b=3;
        System.out.println(a/b);
    }
}
